package com.coocaa.launcher.wallpaper;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.utils.Android;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.service.litecontent.data.store.com_coocaa_theme.RP_WallpaperList;
import com.coocaa.x.uipackage.BaseView;
import com.coocaa.x.uipackage.utils.CoocaaFresco;
import com.coocaa.x.uipackage.widget.CoocaaDraweeView;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.widget.TipToast;

/**
 * Created by luwei on 15-7-4.
 */
public class WallpaperItem extends BaseView {
    private CoocaaDraweeView bg = null;
    private ImageView currentPoint = null;
    private ImageView collectPoint = null;
    private RP_WallpaperList.WallPaper_Item wallPaper_item = null;

    public WallpaperItem(Context context) {
        super(context);
        addBg();
        addCollectPoint();
        setOnClickListener(this);
    }


    private void addBg() {
        bg = new CoocaaDraweeView(mContext);
        bg.setBackgroundResource(R.drawable.launcher_wallpaperitem_default_bg);
        FrameLayout.LayoutParams params = new LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        addView(bg, params);
    }

    private void addCurrentBgPoint() {
        if (currentPoint == null) {
            currentPoint = new ImageView(mContext);
            FrameLayout.LayoutParams params = new LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
            params.leftMargin = CoocaaApplication.Div(15);
            params.bottomMargin = CoocaaApplication.Div(12);
            currentPoint.setImageResource(R.drawable.do_complete);
            addView(currentPoint, params);
        }
    }

    private void addCollectPoint() {
        if (collectPoint == null) {
            collectPoint = new ImageView(mContext);
            FrameLayout.LayoutParams params = new LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM | Gravity.RIGHT);
            params.rightMargin = CoocaaApplication.Div(20);
            params.bottomMargin = CoocaaApplication.Div(20);
            collectPoint.setImageResource(R.drawable.launcher_wallpaper_collection_point);
            addView(collectPoint, params);
        }
    }

    public void setData(RP_WallpaperList.WallPaper_Item item) {
        wallPaper_item = item;
        int width = 384, height = 216;
        String url = wallPaper_item.smallUrl;
        LogUtils.d("wallpaper", "the url is " + url + Uri.parse(url).toString());
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url))
                .setResizeOptions(new ResizeOptions(width, height))
                .build();
        DraweeController controller = CoocaaFresco.newDraweeControllerBuilder(mContext)
                .setOldController(bg.getController())
                .setImageRequest(request)
                .build();
        bg.setController(controller);
        if (wallPaper_item.isCollected) {
            addCollectPoint();
            collectPoint.setVisibility(View.VISIBLE);
        } else {
            if (collectPoint != null)
                collectPoint.setVisibility(View.GONE);
        }
        RP_WallpaperList.WallPaper_Item current = WallpaperManager.getInstance().getCurrentWallpaper();
        if (current != null && current.bigUrl.equals(wallPaper_item.bigUrl)) {
            addCurrentBgPoint();
            currentPoint.setVisibility(View.VISIBLE);
        } else {
            if (currentPoint != null)
                currentPoint.setVisibility(View.GONE);
        }
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (!wallPaper_item.type.equals("ownwallpaper") && !wallPaper_item.isCollected) {
            if (Android.isNetConnected(mContext)) {
                Intent intent = new Intent(mContext, WallpaperPreViewActivity.class);
                intent.putExtra("item", wallPaper_item.toString());
                intent.putExtra("pos", v.getId());
                mContext.startActivity(intent);
            } else {
                TipToast.getInstance(mContext).showTipToast(getResources().getString(R.string.please_connect_net));
            }
        } else {
            Intent intent = new Intent(mContext, WallpaperPreViewActivity.class);
            intent.putExtra("item", wallPaper_item.toString());
            intent.putExtra("pos", v.getId());
            mContext.startActivity(intent);
        }
    }
}
